package com.skyolder.milkbee.web.rest.my;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Favorite;
import com.skyolder.milkbee.service.FavoriteService;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;
import com.skyolder.milkbee.web.util.WebUserUtil;
import com.skyolder.milkbee.service.dto.FavoriteDTO;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * REST controller for managing Favorite.
 */
@RestController("myFavoriteResource")
@RequestMapping("/api/my")
public class FavoriteResource {

    private final Logger log = LoggerFactory.getLogger(FavoriteResource.class);
        
    @Inject
    private FavoriteService favoriteService;

    @Inject
    private WebUserUtil webUserUtil;


    @PostMapping("/favorite/card/{cardId}")
    @Timed
    public ResponseEntity<FavoriteDTO> createFavorite(@PathVariable Long cardId) throws URISyntaxException {
    	//webUserUtil.getCurrentUser()
    	FavoriteDTO favoriteDTO=new FavoriteDTO();
    	favoriteDTO.setCardId(cardId);
    	favoriteDTO.setCreatedAt(ZonedDateTime.now());
    	favoriteDTO.setLeaguerId(webUserUtil.getCurrentLeaguer().getId());
    	favoriteDTO.setUserId(webUserUtil.getCurrentUser().getId());
    	Favorite favorite=favoriteService.findByCardIdAndUserId(cardId,webUserUtil.getCurrentUser().getId());
    	if(favorite!=null){
    	       return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("favorite", "idexists", "A new favorite cannot already have an ID")).body(null);
    	}
    	FavoriteDTO result = favoriteService.save(favoriteDTO);
    	return ResponseEntity.created(new URI("/api/favorites/" + result.getId()))
    			.headers(HeaderUtil.createEntityCreationAlert("favorite", result.getId().toString()))
    			.body(result);
    }

    /**
     * GET  /favorites : get all the favorites.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of favorites in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/favorites")
    @Timed
    public ResponseEntity<List<FavoriteDTO>> getAllFavorites(@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Favorites");
        
        //Page<HashMap<String,Object>> page = 
        Page<FavoriteDTO> page=	favoriteService.queryByUserId(webUserUtil.getCurrentUser().getId(),pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/my/favorites");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }



    /**
     * DELETE  /favorites/:id : delete the "id" favorite.
     *
     * @param id the id of the favoriteDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/favorites/{id}")
    @Timed
    public ResponseEntity<Void> deleteFavorite(@PathVariable Long id) {
        log.debug("REST request to delete Favorite : {}", id);
        favoriteService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("favorite", id.toString())).build();
    }

}
